package pmg;

import java.awt.Color;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;

/**
 * Modified version of sample robot Walls. Based on M. Nelson and F. Larsen's Walls.
 * 
 * @author Paul Galiza
 * @version 1.1
 */
public class WallEE extends Robot {
  boolean peek; // Don't turn if there's a robot there
  double moveAmount; // How much to move

  /**
   * run: Move around the walls.
   */
  public void run() {
    // Set colors
    setBodyColor(Color.orange);
    setGunColor(Color.orange);
    setRadarColor(Color.red);
    setBulletColor(Color.orange);
    setScanColor(Color.red);

    // Initialize moveAmount to the maximum possible for this battlefield.
    moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
    // Initialize peek to false
    peek = false;

    // turnLeft to face a wall.
    // getHeading() % 90 means the remainder of
    // getHeading() divided by 90.
    turnLeft(getHeading() % 90);
    ahead(moveAmount);
    // Turn the gun to turn right 90 degrees.
    peek = true;
    turnGunRight(90);
    turnRight(90);

    while (true) {
      int counter = 0;
      while (counter < 4) {
        // Look before we turn when ahead() completes.
        peek = true;
        // Move up the wall
        ahead(moveAmount);
        // Don't look now
        peek = false;
        // Turn to the next wall
        turnRight(90);
        counter++;
      }
      ahead(moveAmount / 2);
      turnRight(90);
    }
  }

  /**
   * onHitRobot: Move away a bit.
   * 
   * @param e event
   */
  public void onHitRobot(HitRobotEvent e) {
    // If he's in front of us, set back up a bit.
    if (e.getBearing() > -90 && e.getBearing() < 90) {
      back(100);
    } // else he's in back of us, so set ahead a bit.
    else {
      ahead(100);
    }
  }

  /**
   * onHitByBullet: Turn right.
   * 
   * @param e event
   */
  public void onHitByBullet(HitByBulletEvent e) {
    turnRight(90);
  }

  /**
   * onScannedRobot: Fire!
   * 
   * @param e event
   */
  public void onScannedRobot(ScannedRobotEvent e) {
    double distance = e.getDistance();
    specialFire(distance);

    back(moveAmount / 2);
    ahead(moveAmount);

    // Note that scan is called automatically when the robot is moving.
    // By calling it manually here, we make sure we generate another scan event if there's a robot
    // on the next
    // wall, so that we do not start moving up it until it's gone.
    if (peek) {
      scan();
    }
  }

  /**
   * Fires an appropriate bullet power based on distance.
   * 
   * @param distance between my robot and the enemy
   */
  private void specialFire(double distance) {
    if (distance <= 100) {
      fire(3);
      scan();
    }
    else if (distance > 100 && distance < 300) {
      fire(2);
      scan();
    }
    else {
      fire(1);
      scan();
    }
  }
}
